//
//  Home_PtViewController.m
//  TY-C
//
//  Created by 张雅 on 2024/3/17.
//

#import "Home_PtViewController.h"
#import "ComonFile.h"
#import "Home_BottomMesView.h"

@interface Home_PtViewController ()<YBPopupMenuDelegate,UIScrollViewDelegate,ChartViewDelegate,IChartAxisValueFormatter>
///反馈阀值
@property (nonatomic,strong) UILabel * topLab;
///反馈阀值选择框
@property (nonatomic,strong) UIButton * topBtn;

@property (nonatomic,strong) LineChartView * chartView0;
@property (nonatomic,strong) LineChartDataSet * dataSet;

@property (nonatomic,strong) Home_BottomMesView * mesView;

///
@property (nonatomic,strong) UIButton * outBtn;
///
@property (nonatomic,strong) UIButton * startBtn;


@end

@implementation Home_PtViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTopView];
    [self initLineView];
    [self initMesView];
    [self initBottomBtn];
}

- (void)initBottomBtn {
    
    _outBtn = [UIButton new];
    _startBtn = [UIButton new];
    
    [self.view addSubview:_outBtn];
    [self.view addSubview:_startBtn];
    
    [_outBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80*IPADSCALE));
        make.height.equalTo(@(30*IPADSCALE));
        make.bottom.equalTo(self.view.mas_bottom).offset(-20*IPADSCALE);
        make.right.equalTo(_mesView.mas_centerX).offset(-15);
    }];
    
    
    [_startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.bottom.equalTo(_outBtn);
        make.left.equalTo(_mesView.mas_centerX).offset(15);
    }];
    
    
    [_outBtn addTarget:self action:@selector(outBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_startBtn addTarget:self action:@selector(outBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    [_outBtn setBackgroundImage:[UIImage imageNamed:@"btn_exit"] forState:0];
    [_startBtn setBackgroundImage:[UIImage imageNamed:@"btn_start"] forState:0];
    [_startBtn setBackgroundImage:[UIImage imageNamed:@"btn_pause"] forState:UIControlStateSelected];
    _startBtn.selected = NO;

}

- (void)outBtnAction:(UIButton *)btn {
    if(btn == _outBtn) {
        ///退出
        Pac_TipsSureBtn * ppp = [[Pac_TipsSureBtn alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [ppp sureBtnwithDoneBlock:^(Pac_TipsSureBtn * _Nonnull pickerBtn, NSInteger selectedIndex, id  _Nonnull selectedValue) {
            NSLog(@"%@,%ld",self,selectedIndex);
            ///应该是crash。但是在crash之前 一定要关闭蓝牙或者听着蓝牙操作
        } CancelBlock:^(Pac_TipsSureBtn * _Nonnull pickerBtn) {
    
        }];
        
        
        
        
    }else{
        if(btn.selected == YES){
            ///暂停操作
            NSLog(@"暂停");

        }else{
            ///开始操作
            NSLog(@"开始");
        }
        btn.selected = !btn.selected;
    }
}

- (void)initMesView {
    _mesView = [[Home_BottomMesView alloc]initWithFrame:CGRectMake(25 * IPADSCALE, CGRectGetMaxY(_chartView0.frame) + 21*IPADSCALE, self.viewWidth - 25 * IPADSCALE * 2, 52 * IPADSCALE)];
    [self.view addSubview:_mesView];
}

- (void)initTopView {
    _topLab =[UILabel new];
    _topBtn = [UIButton new];
    [self.view addSubview:_topBtn];
    [self.view addSubview:_topLab];
    
    
    _topLab.frame = CGRectMake(25 * IPADSCALE, 20*IPADSCALE, 50*IPADSCALE, 14.5*IPADSCALE);
    _topLab.text = @"反馈阈值";
    _topLab.font = ZYQFONT(20);
    _topLab.textColor = RGBColor(102, 102, 102);
    
    _topBtn.frame = CGRectMake(CGRectGetMaxX(_topLab.frame) + 3.5, 18*IPADSCALE, 73*IPADSCALE, 22*IPADSCALE);
    [_topBtn setBackgroundImage:[UIImage imageNamed:@"img_select"] forState:0];
    [_topBtn setTitle:@" 低" forState:0];
    _topBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    _topBtn.titleLabel.font = ZYQFONT(20);
    [_topBtn setTitleColor:RGBColor(51, 51, 51) forState:0];
    [_topBtn addTarget:self action:@selector(topBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)topBtnAction {
    [YBPopupMenu showRelyOnView:_topBtn titles:@[@" 低",@" 中",@" 高"] icons:nil menuWidth:CGRectGetWidth(_topBtn.frame) otherSettings:^(YBPopupMenu *popupMenu) {
        popupMenu.delegate = self;
        popupMenu.tag = 3002;
        popupMenu.font = ZYQFONT(20);
    }];
    
}

- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index {
    NSArray * array11 = @[@" 低",@" 中",@" 高"];
    [_topBtn setTitle:array11[index] forState:0];
}


- (void)initLineView {
    _chartView0 = [[LineChartView alloc]initWithFrame:CGRectMake(25*IPADSCALE, CGRectGetMaxY(_topBtn.frame)+28, self.viewWidth - 25* IPADSCALE * 2, 165*IPADSCALE)];
    _chartView0.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_chartView0];
    _chartView0.delegate = self;
    _chartView0.xAxis.valueFormatter = self;
    // 距离边缘的间隙
    [_chartView0 setExtraOffsetsWithLeft:15 top:20 right:15 bottom:0];
    // noDataText 没有数据的时候的展示
    _chartView0.noDataText = @"";
    _chartView0.dragEnabled = YES;
    _chartView0.chartDescription.enabled = YES;
    _chartView0.pinchZoomEnabled = NO;


    _chartView0.drawGridBackgroundEnabled = NO;
    _chartView0.autoScaleMinMaxEnabled = YES;
//    _chartView0.maxHighlightDistance = 300;
    //   取消XY轴缩放 不能缩放

    _chartView0.scaleYEnabled = NO;

    _chartView0.scaleXEnabled = NO;

    ///图例
    _chartView0.legend.enabled = NO;

    // 取消双击缩放
    _chartView0.doubleTapToZoomEnabled = NO;
    
    // 拖拽后惯性效果的摩擦系数(0~1)，数值越小，惯性越不明显
//    self.chartView0.dragEnabled = YES;//启用拖拽图标
    self.chartView0.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
    _chartView0.dragDecelerationFrictionCoef = 0.9;
    
    
//    ------------------ Y轴设置----------------

    // 是否绘制右边轴

    _chartView0.rightAxis.enabled = NO;
    // 获取左边Y轴
    ChartYAxis *leftAxis = _chartView0.leftAxis;

    // Y轴label数量，数值不一定，如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均

    leftAxis.labelCount = 6;

    // 是否强制绘制指定数量的labe

    leftAxis.forceLabelsEnabled = YES;
    leftAxis.axisLineWidth= 0.0;

    
    
    // 设置Y轴的最小最大值

    leftAxis.axisMaximum = 100;
//
    leftAxis.axisMinimum = 0;
    // 是否将Y轴进行上下翻转

    leftAxis.inverted = NO;
    
    
    // Y轴label文字颜色

    leftAxis.labelTextColor= RGBColor(102, 102, 102);
    leftAxis.labelFont = ZYQFONT(14);
    leftAxis.granularityEnabled = NO;                      // 是否开启j抗锯齿，默认打开
//    leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];
//    leftAxis.gridColor = [UIColor redColor];



    
    ///x轴
    ChartXAxis *xAxis = _chartView0.xAxis;
    xAxis.labelFont = ZYQFONT(14);
    xAxis.labelTextColor= RGBColor(102, 102, 102);
    xAxis.labelWidth = 1;
    
//    xAxis.gridLineDashLengths = @[@3.0f, @3.0f];
    // X网格线颜色
//    xAxis.gridColor = [UIColor redColor];
    xAxis.drawGridLinesEnabled = YES;///网格线是否显示

    xAxis.labelPosition = XAxisLabelPositionBottom;

    //强制显示制定个数。默认NO
//    xAxis.forceLabelsEnabled = YES;
    ///很重要，会显示出来多少个点
    xAxis.axisMaximum = 1000;
    xAxis.axisMinimum = 0;
    xAxis.labelCount = 6 ;                              // 强制固定label个数
    [_chartView0 setVisibleXRangeWithMinXRange:0 maxXRange:31];

//    xAxis.avoidFirstLastClippingEnabled = YES;
    //不强制绘制指定数量的label
//    X轴间隔
//    _chartView0.xAxis.granularity = 1;
    
    NSMutableArray * mu = [NSMutableArray new];
    NSMutableArray * mu111 = [NSMutableArray new];

    for (int i = 0; i<10; i++) {
        
        ChartDataEntry * en = [[ChartDataEntry alloc]initWithX:(double)i y:(double)i+10];
        [mu addObject:en];
        
        ChartDataEntry * en111 = [[ChartDataEntry alloc]initWithX:(double)i y:(double)i+20];
        [mu111 addObject:en111];
        
    }
    _dataSet = [[LineChartDataSet alloc]initWithEntries:mu label: @""];
    LineChartDataSet * dddset = [[LineChartDataSet alloc]initWithEntries:mu111 label: @""];
    
    
//    self->_dataSet = [[LineChartDataSet alloc]initWithEntries:mu];///添加 ChartDataEntry数据
//    [self->_dataSet setvalu]
    self->_dataSet.lineWidth = 2.0f;//折线宽度
    [self.dataSet setColor:RGBColor(65, 185, 98)];
    self->_dataSet.drawValuesEnabled = NO;//是否在拐点处显示数据
//            self->_dataSet.valueColors = @[[UIColor orangeColor],[UIColor redColor]];//折线拐点处显示数据的颜色
    self->_dataSet.drawCirclesEnabled = NO;//是否开启绘制阶梯样式的折线图
    self->_dataSet.cubicIntensity = 0.2;// 曲线弧度
    self->_dataSet.circleRadius = 3.0f;//拐点半径
    self->_dataSet.mode = LineChartModeCubicBezier;// 模式为曲线模式
    self->_dataSet.axisDependency = AxisDependencyLeft;
//    self->_dataSet.circleHoleRadius = 2.0f;//空心的半径
//    self->_dataSet.circleColors = @[[UIColor blueColor]];//空心的圈的颜色
//    self->_dataSet.circleHoleColor = [UIColor grayColor];//空心的颜色
//    self->_dataSet.highlightEnabled = YES;//选中拐点,是否开启高亮效果(显示十字线)
    self->_dataSet.highlightColor = [UIColor clearColor];
    self->_dataSet.valueFont = [UIFont systemFontOfSize:12];
    self->_dataSet.drawFilledEnabled = NO;//是否填充颜色
    
    LineChartData  * data = [[LineChartData alloc]initWithDataSet:self->_dataSet];
    
    
    
    LineChartData * dadd = [[LineChartData alloc]initWithDataSets:@[self.dataSet,dddset]];
    _chartView0.data = dadd;
//    [_chartView0.data notifyDataChanged];
//    [_chartView0 notifyDataSetChanged];
//    [self.chartView0 invalidateIntrinsicContentSize];
//    [_chartView0 moveViewToX:0];///位移到0
    ///
//    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
//
//    [dataSetsaddObject:lineSet];
//
//    [dataSetsaddObject:lineSet2];
//
//    LineChartData*data = [[LineChartDataalloc]initWithDataSets:dataSets];
//
//    self.lineChartView.data =data;

    
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis{
//    NSLog(@"%if",value);
//    NSString * str =[NSString stringWithFormat:@"%@~", self.dataArray[(int)value % self.dataArray.count]];
    NSString * sss =  [NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"%%.%if", 1]];
    NSString * s = [NSString stringWithFormat:sss,value];
    NSLog(@"%@",s);
    NSInteger ttt = [s intValue];
    if(ttt % 6 == 0){
        return s;
    }else{
        return s;
    }
    return s;
//    return  [NSString stringWithFormat:[NSString stringWithFormat:@"%%.%if", 0] , value];

    

    

}

@end
